<?php
namespace app\index\controller;

use think\Db;
use think\Session;
use app\common\controller\Base;

class Login extends Base
{
    public function index()
    {
        return $this->fetch('login/login');
    }

    public function login(){
        if(request()->isPost()){
            $data = input('post.');
            
            $user = Db::name('user')->field("id,password,pwd_code,nickname,account,status")->where( array("account"=>$data['account']))->find();
            if( empty($user["status"])){
                $this->error("你的账号已被收回，请联系客服！",'Login/index');
            }
            if(getMd5Pas($data['pwd'],$user['pwd_code']) == $user['password']){
                $list = array();
                $list['id'] = $user['id'];
                $list['login_time'] = time();
                $list['login_ip'] = get_client_ip();
                Db::name('user')->update($list);

                $row = array("id"=>$user['id'],"account"=>$user['account'],"nickname"=>$user['nickname']);

                Session::set('userInfo',$row);
                $this->redirect('User/index');
            }else{
                $this->error("登录失败,用户名或密码错误!",'Login/index');
            }
        }
    }

    public function forget_pas(){

        if( request()->isPost() ){

            $map = input('post.');

            $code = Session::get('forget_code');
            //print_r($code);exit;
            if( $code != $map["forget_code"] ){
                $retArr = [
                    'result' => 3,
                    'msg' => '验证码错误',
                ];
                $this->json_return($retArr);
            }
            
            $Arr = Db::name("User")->field("id,account,nickname")->where("account",$map["account"])->find();
            if( empty($Arr) ){
                $retArr = [
                    'result' => 1,
                    'msg' => '没有此账号',
                ];
                $this->json_return($retArr);
            }
            $pas_salt = getRandChar(6);
            $data = array(
                'password' => getMd5Pas($map["pwd"], $pas_salt),
                'pwd_code' => $pas_salt,
                'login_time' => time(),
                'login_ip' => get_client_ip()
            );
            //print_r($Arr);exit;

            $pd = Db::name("User")->where("id",$Arr["id"])->update($data);
            if($pd){
                Session::set('userInfo',$Arr);
                $retArr = [
                    'result' => 0,
                    'msg' => '修改密码成功',
                ];
                $this->json_return($retArr);
            }else{
                $retArr = [
                    'result' => 1,
                    'msg' => '操作失败',
                ];
                $this->json_return($retArr);
            }
        }

        $map = input("param.");
        $account = "";
        if( isset($map["account"]) && !empty($map["account"]) ) {
            $account = $map["account"];
        }

        Session::delete('forget_code');
        $this->assign("account",$account);
        return $this->fetch();
    }

    function get_forget_code(){
        $map = input('post.');
        if( empty($map["account"]) ){
            $retArr = [
                'result' => 1,
                'msg' => '请输入账号',
            ];
            $this->json_return($retArr);
        }

        $Arr = Db::name("User")->field("id,phone")->where("account",$map["account"])->find();
        if( empty($Arr) ){
            $retArr = [
                'result' => 1,
                'msg' => '你输入的账号还没有注册！',
            ];
            $this->json_return($retArr);
        }

        $code = rand(100000,999999);
        //加载common目录下的TransmitSms

        $sms = app_service("common","TransmitSms");
        //print_r($sms);exit;
        $pd = $sms->phone_validate($Arr["phone"],$code);
        if($pd){
            Session::set('forget_code',$code);
            $retArr = [
                "result" => 0,
                "msg" => "发送成功",
            ];
        }else{
            $retArr = [
                "result" => 1001,
                "msg" => "发送失败",
            ];
        }
        $this->json_return($retArr);

    }

    
    public function logout(){
        Session::delete('userInfo');
        
        $this->redirect("Login/index");
    }
}